package com.koron.css2.newProjectCenter.impl.query.statistics;

import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.impl.query.statistics.bean.PjStatWatchInstallBean;
import com.koron.css2.newProjectCenter.impl.query.statistics.vo.PjStatWatchInstallVo;
import com.koron.css2.newProjectCenter.impl.query.statistics.vo.PjStatWatchInstallVoDetail;
import com.koron.css2.newProjectCenter.mapper.PjStatWatchInstallApplyMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

public class PjStatWatchInstallApply implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        PjStatWatchInstallBean bean = JsonUtils.objectToPojo(req.getData(), PjStatWatchInstallBean.class);
        PjStatWatchInstallApplyMapper mapper = factory.getMapper(PjStatWatchInstallApplyMapper.class);
        if (bean.getBusinessArea() == null) {
            return MessageBean.create(Constant.ILLEGAL_PARAMETER, "参数错误", null);
        }

        List<PjStatWatchInstallVoDetail> list = mapper.getList(bean);
        PjStatWatchInstallVo vo = new PjStatWatchInstallVo();
        vo.setList(list);
        Integer sum = 0;
        for (PjStatWatchInstallVoDetail detail : list) {
            sum += detail.getWatch();
        }
        vo.setSum(sum);
        vo.setBusinessArea(bean.getBusinessArea());
        MessageBean result = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PjStatWatchInstallVo.class);
        result.setData(vo);
        return result;
    }
}
